<template>
  <div id="home">
    <nav-bar class="home-nav"> <div slot="center">购物车</div></nav-bar>
    <tab-control
      class="tab-conntrol"
      :titles="['流行', '新款', '精选']"
      @tabClick="tabClick"
      ref="tabControl1"
      v-show="isTabFixed"
    ></tab-control>
    <!-- v-bind 为了确定类型 Number-->
    <scroll
      class="container"
      ref="scroll"
      :probe-type="3"
      @scroll="contentScroll"
      :pull-up-load="true"
      @pullingUp="loadMore"
    >
      <home-swiper
        :banners="banners"
        @swiperImageLoad="swiperImageLoad"
      ></home-swiper>
      <reconmmend-view :recommends="recommends"></reconmmend-view>
      <feature-view />
      <tab-control
        :titles="['流行', '新款', '精选']"
        @tabClick="tabClick"
        ref="tabControl2"
      ></tab-control>
      <goods-list :goods="showGoods"> </goods-list>
    </scroll>
    <!-- 监听组件根元素的原生事件 -->
    <back-top v-show="isShowBackTop" @click.native="backClick" />
  </div>
</template>

<script>
import HomeSwiper from "./childcomps/HomeSwiper.vue";
import ReconmmendView from "./childcomps/ReconmmendView.vue";
import FeatureView from "./childcomps/FeatureView.vue";

import NavBar from "components/common/navbar/NavBar";
import Scroll from "components/common/scroll/Scroll";
import TabControl from "components/content/tabControl/TabControl.vue";
import GoodsList from "components/content/goods/GoodsList.vue";

import { getHomeMultidata, getHomeGoods } from "network/home";
import { debounce } from "common/utils";
import { itemListenerMixin, backTopMixin } from "common/mixin";

export default {
  data() {
    return {
      banners: [],
      recommends: [],
      goods: {
        pop: { page: 0, list: [] },
        new: { page: 0, list: [] },
        sell: { page: 0, list: [] },
      },
      currentType: "pop",
      isShowBackTop: false,
      tabOffsetTop: 0,
      isTabFixed: false,
      saveY: 0,
    };
  },
  mixins: [itemListenerMixin, backTopMixin],
  components: {
    NavBar,
    HomeSwiper,
    ReconmmendView,
    FeatureView,
    TabControl,
    GoodsList,
    Scroll,
  },
  computed: {
    showGoods() {
      return this.goods[this.currentType].list;
    },
  },
  created() {
    //请求多个数据
    this.getHomeMultidata();
    //请求商品数据
    this.getHomeGoods("pop");
    this.getHomeGoods("new");
    this.getHomeGoods("sell");
  },
  mounted() {
    //混入
    // //1、图片加载完成的事件监听
    // //传入函数 而不是传入返回值 不需要小括号
    // const refresh = debounce(this.$refs.scroll.refresh, 10);
    // this.itemImgListener = () => {
    //   refresh();
    //   // this.$refs.scroll.refresh();
    // };
    // //监听item中图片加载完成
    // this.$bus.$on("itemImageLoad", this.itemImgListener);
  },
  activated() {
    this.$refs.scroll.scrollTo(0, this.saveY, 1);
    this.$refs.scroll.refresh();
    // console.log("activated");
  },
  deactivated() {
    //1、保存Y值
    this.saveY = this.$refs.scroll.getScrollY();
    //2、取消全局监听 传入事件 和函数
    this.$bus.$off("itemImageLoad", this.itemImgListener);
  },
  destroyed() {
    console.log("home destroyed");
  },

  methods: {
    swiperImageLoad() {
      //获取tabControl的offsetTop
      this.tabOffsetTop = this.$refs.tabControl2.$el.offsetTop;
      console.log(this.$refs.tabControl2.$el.offsetTop);
    },
    loadMore() {
      setTimeout(() => {
        this.getHomeGoods(this.currentType);
        //完成上拉加载更多
        this.$refs.scroll.finishPullUp();
      }, 1000);
    },
    contentScroll(position) {
      //1、判断BackTop是否显示
      this.isShowBackTop = -position.y > 1000;
      //2、决定tabcontrol是否吸顶（position:fixed）
      this.isTabFixed = -position.y > this.tabOffsetTop;
    },

    //混入
    // backClick() {
    //   this.$refs.scroll.scrollTo(0, 0, 3000);
    // },
    /**
      事件监听的方法
     */
    tabClick(index) {
      // this.currentType = Object.keys(this.goods)[index]
      switch (index) {
        case 0:
          this.currentType = "pop";
          break;
        case 1:
          this.currentType = "new";
          break;
        case 2:
          this.currentType = "sell";
          break;
      }
      this.$refs.tabControl1.currentIndex = index;
      this.$refs.tabControl2.currentIndex = index;
    },

    /**
    网络请求的方法
     */
    getHomeMultidata() {
      getHomeMultidata()
        .then((result) => {
          // console.log(result);
          this.banners = result.data.banner.list;
          this.recommends = result.data.recommend.list;
        })
        .catch((err) => {
          return err;
        });
    },
    getHomeGoods(type) {
      //不理解
      const page = this.goods[type].page + 1;
      getHomeGoods(type, page).then((res) => {
        // console.log(res);
        this.goods[type].list.push(...res.data.list);
        this.goods[type].page += 1;
        //异步操作
      });
    },
  },
};
</script>

<style scope>
#home {
  /* padding-top: 44px; */
  height: 100vh;
  position: relative;
}
.home-nav {
  background-color: var(--color-tint);
  color: #fff;
  /* position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 9; */
}
.tab-control {
  /* position: sticky; 吸顶 */
  position: relative;
  top: -1px;
  z-index: 9;
}

/* 第一种计算高度的方式 */
/* .container {
  有
  height: calc(100% - 93px);
  overflow: hidden;
  margin-top: 44px;
} */
/* 第二种计算高度的方式 */

.container {
  overflow: hidden;
  position: absolute;
  top: 43px;
  bottom: 49px;
  left: 0;
  right: 0;
}
</style>
